<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <brother1></brother1>
        <brother2></brother2>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        // 事件车
        var eventBus = new Vue();
        // $on   $emit 

        let brother1 = {
            template: "<div :style='{color:color}'>brother1<button @click='changeGreen'>变绿</button></div>",
            created() {
                eventBus.$on("red", ()=> {
                    this.color = "red"
                })
            },
            data() {
                return {
                    color: ""
                }
            },
            methods: {
                changeGreen() {
                    // 发布green事件
                    eventBus.$emit("green")
                }
            }
        }

        let brother2 = {
            template: "<div :style='{color:color}'>brother2<button @click='changeRed'>变红</button></div>",
            data() {
                return {
                    color:""
                }
            },
            methods: {
                changeRed() {
                    eventBus.$emit("red")
                }
            },
            created() {
                // 订阅green事件
                eventBus.$on("green", ()=> {
                    this.color = "green"
                })
            }
        }

        const app = new Vue({
            el: '#app',
            components: {
                brother1,
                brother2
            }
        })
    </script>
</body>
</html>